package com.common.config;

import com.common.eunm.ExceptionCode;
import com.common.exception.TokenExpireException;
import com.common.result.ResponseVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/*
* 全局异常处理类 (AOP)
* 后面可以自定义异常来处理
* */
@RestControllerAdvice(basePackages = {"com.example.pluginmanager.controller","com.example.pluginmanager.interceptor"})
@Component
@Slf4j
public class GlobalExceptionHandler {
    @ExceptionHandler(RuntimeException.class)
    public ResponseVO runtimeExceptionHandler(RuntimeException e){
        log.error("RuntimeException: {}",e.getMessage());
        return ResponseVO.error(e.getMessage());
    }

    @ExceptionHandler(TokenExpireException.class)
    public ResponseVO tokenExpireHandler(TokenExpireException tokenExpireException){
        log.error("TokenExpireHandler: {}",tokenExpireException.getMessage());
        return ResponseVO.error(tokenExpireException.getMessage(), ExceptionCode.TOKEN_EXPIRE_EXEC.getCode());
    }
}
